//
//  DYZDealedController.m
//  DYZCars
//
//  Created by 李凯 on 16/8/20.
//  Copyright © 2016年 MF. All rights reserved.
//

#import "DYZDealedController.h"
#import "DYZCarShowModel.h"
#import "DYZNewLaunchCell.h"
#import "DSHTTPClient.h"
#import "DYZDetailedInfoController.h"

@interface DYZDealedController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSArray *carShowArr;
@property (nonatomic, strong) UITableView *tableView;

@end

//static NSString *newLaunchCellID = @"newLaunchCellID";

@implementation DYZDealedController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self loadData];
    [self setupTableView];
}

- (void)loadData
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    // type = 0/1
    [param setValue:@"1" forKey:@"type"];
    
    
    [DSHTTPClient postUrlString:@"http://iosapi.itcast.cn/car/searchlist.json.php" withParam:param withSuccessBlock:^(id data) {
        NSDictionary *dict = [data objectForKey:@"data"];
        NSArray *arr = [NSArray yy_modelArrayWithClass:[DYZCarShowModel class] json:[dict objectForKey:@"carList"]];
        //        NSLog(@"%@", arr);
        self.carShowArr = arr.copy;
        [self.tableView reloadData];
    } withFailedBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } withErrorBlock:^(NSString *message) {
        NSLog(@"%@",message);
    }];
    
    //    NSString *path = [[NSBundle mainBundle]pathForResource:@"CarShow.plist" ofType:nil];
    //    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    //    for (NSDictionary *dict in arr) {
    //        DYZCarShowModel *model = [[DYZCarShowModel alloc]init];
    //        [model setValuesForKeysWithDictionary:dict];
    //        //        NSLog(@"%@", model);
    //        [self.carShowArr addObject:model];
    //    }
    //    [self.tableView reloadData];
}

- (void) setupTableView
{
    UITableView *tableView = [[UITableView alloc]init];
    [tableView registerNib:[UINib nibWithNibName:@"DYZNewLaunchCell" bundle:nil] forCellReuseIdentifier:@"newLaunchCellID"];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.rowHeight = 64;
    //    self.view = tableView;
    [self.view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.offset([UIScreen mainScreen].bounds.size.height-120);
    }];
    self.tableView = tableView;
    //隐藏多余的tableViewCell
    tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
}

#pragma mark - UITabelViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.carShowArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DYZNewLaunchCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newLaunchCellID" forIndexPath:indexPath];
    cell.model = self.carShowArr[indexPath.row];
    return cell;
}

#pragma mark - UITabelViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DYZDetailedInfoController *vc = [[DYZDetailedInfoController alloc]init];
    [self.navigationController pushViewController:vc animated:true];
}

//懒加载
-(NSArray *)carShowArr
{
    if(_carShowArr == nil)
    {
        _carShowArr = [NSMutableArray array];
    }
    return _carShowArr;
}


@end

